package cn.xspace.library.util;

import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.xspace.android.xafcommon.been.UserAuthorityBean;
import com.xspace.android.xafcommon.util.JSONUtil;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.List;

public class AuthorityController {

    private static AuthorityController controller;
    /**
     * 是否个人权限可用
     **/
    private static final boolean isEnableUserAuthority = true;
    /**
     * 是否机构权限可用
     **/
    private static final boolean isEnableOrgAuthority = true;

    public static AuthorityController getInstance() {
        if (controller == null) controller = new AuthorityController();
        return controller;
    }

    private void parseAuthorityData(String jsonData) {
        if (TextUtils.isEmpty(jsonData)) return;
        List<UserAuthorityBean> authList = JSONUtil.parseJsonToList(jsonData, new TypeToken<List<UserAuthorityBean>>() {
        }.getType());
        MyInfoUtil.setAuthMap(authList);
    }

    private boolean getAuthorityMapData() {
        if (BuildCache.mBuildConfig.authorityMap.size() == 0) {
            parseAuthorityData(MyInfoUtil.getUserAuthorityData());
        }
        return (BuildCache.mBuildConfig.authorityMap.size() > 0);
    }

    public boolean isCanViewLogDirectDept() {
        if (!isEnableUserAuthority) return true;
        boolean isEnable = false;
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ViewDirdeptWorklog)) {
                isEnable = true;
            }
        }
        return isEnable;
    }

    public boolean isCanCheckOtherLog() {
        if (!isEnableUserAuthority) return true;
        boolean isEnable = false;
        if (isCanCheckDeptLog() || isCanViewLogDirectDept()) {
            isEnable = true;
        }
        return isEnable;
    }

    public boolean isCanCheckDeptLog() {
        if (!isEnableUserAuthority) return true;
        boolean isEnable = false;
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ViewDeptuserWorklog)) {
                isEnable = true;
            }
        }
        return isEnable;
    }

    public boolean isCanCheckLogStatistic() {
        if (!isEnableUserAuthority) return true;
        boolean isEnable = false;
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ViewJianguanStatistic)) {
                isEnable = true;
            }
        }
        return isEnable;
    }

    //-----------------------------------机构权限部分-----------------------------------------------------------------------
    public boolean isCanViewAddressbookCircle() {
        if (!isEnableOrgAuthority) return true;
        boolean isEnable = false;
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ChatWithGroupUser)) {
                isEnable = true;
            }
        }
        return isEnable;
    }

    public boolean isCanViewAddressbookMyOrg() {
        if (!isEnableOrgAuthority) return true;
        boolean isEnable = false;
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ChatWithSelfDept)) {
                isEnable = true;
            }
        }
        return isEnable;
    }

    /***
     * 是否璧山特殊权限
     **/
    public boolean iSBiShanAppOrgAndDirect() {
        if (!isEnableOrgAuthority) return true;
        boolean isEnable = false;
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.BiShanAppOrgAndDirect)) {
                isEnable = true;
            }
        }
        LogUtil.d("iSBiShanAppOrgAndDirect  " + isEnable);
        return isEnable;
    }

    /**
     * 判断聊天是否有上级直属机构权限
     */
    public boolean isUpDirDept() {
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ChatWithUpDirDept)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断聊天是否有上级归口机构权限
     */
    public boolean isUpBelong() {
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ChatWithUpBelongDept)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断聊天是否有同级直属机构权限
     */
    public boolean isEquivDirDept() {
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ChatWithEquivDirDept)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断聊天是否有下级直属机构权限
     */
    public boolean isDirDept() {
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ChatWithDirDept)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断聊天是否有下级归口权限
     */
    public boolean isBelong() {
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ChatWithBelong)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断工作日志归口权限
     */
    public boolean workLogIsBelong() {
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.WorklogBelong)) {
                return true;
            }
        }
        return false;

    }

    /**
     * 查看直属机构
     **/
    public boolean isCanViewAddressbookDirectDept() {
        if (!isEnableOrgAuthority) return true;
        boolean isEnable = false;
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ChatWithDirDept)) {
                isEnable = true;
            }
        }
        return isEnable;
    }

    public boolean isCanChatWithServiceObj() {
        if (!isEnableOrgAuthority) return true;
        boolean isEnable = false;
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ChatWithServiceObj)) {
                isEnable = true;
            }
        }
        return isEnable;
    }

    /**
     * 判断聊天是否有上级直属机构权限
     */
    public boolean isShowMonitorApp() {
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.ViewMonitorApp)) {
                return true;
            }
        }
        return false;
    }

    public boolean isShowJinayiForm() {
        if (getAuthorityMapData()) {
            if (BuildCache.mBuildConfig.authorityMap.containsKey(MyInfoUtil.JianYiForm)) {
                return true;
            }
        }
        return false;
    }
}
